package com.ebt.m.repository.provider;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.data.bean.EbtFile;
import com.ebt.m.repository.listener.IconLoadFinishListener;
import com.ebt.m.repository.provider.FileTypeHelper.FileTypeAll;
import com.ebt.m.utils.imageloader.ImageLoader;
import com.ebt.m.utils.imageloader.ImageLoaderUtil;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class FileIconHelper implements IconLoadFinishListener {
    private Context context;
    private FileIconLoader mIconLoader;

    public FileIconHelper(Context context) {
        this.context = context;
        mIconLoader = new FileIconLoader(context, this);
    }

    public static HashMap<FileTypeAll, String[]> getFileExtTypes() {
        return fileExtTypes;
    }

    /* 文件类型集合 */
    public static HashMap<FileTypeAll, String[]> fileExtTypes = new HashMap<FileTypeAll, String[]>();

    static {
        fileExtTypes.put(FileTypeAll.FILE_TYPE_IMAGE, new String[]{"jpg", "jpeg", "gif", "png", "bmp", "wbmp"});
        fileExtTypes.put(FileTypeAll.FILE_TYPE_AUDIO, new String[]{"mp3", "ogg", "wma", "wav", "mid"});
        fileExtTypes.put(FileTypeAll.FILE_TYPE_VIDEO, new String[]{"mp4", "wmv", "mpg", "mpeg", "m4v", "3gp", "3gpp", "3g2", "3gpp2", "asf", "rmvb"});
        fileExtTypes.put(FileTypeAll.FILE_TYPE_PPT, new String[]{"ppt", "pptx"});
        fileExtTypes.put(FileTypeAll.FILE_TYPE_WORD, new String[]{"doc", "docx"});
        fileExtTypes.put(FileTypeAll.FILE_TYPE_EXCEL, new String[]{"xls", "xlsx"});
        fileExtTypes.put(FileTypeAll.FILE_TYPE_APK, new String[]{"apk"});
        fileExtTypes.put(FileTypeAll.FILE_TYPE_PDF, new String[]{"pdf"});
        fileExtTypes.put(FileTypeAll.FILE_TYPE_WEB, new String[]{"htm", "html"});
        fileExtTypes.put(FileTypeAll.FILE_TYPE_TEXT, new String[]{"txt", "log", "xml", "ini", "lrc"});
    }

    /**
     * 根据后缀名获取类型
     */
    public static FileTypeAll getFileTypeBySuffix(String filePath) {
        String extFromFilename = Util.getExtFromFilename(filePath);
        String[] tmps = null;
        Iterator it = fileExtTypes.entrySet().iterator();
        while (it.hasNext()) {
            Entry entry = (Entry) it.next();
            tmps = (String[]) entry.getValue();
            if (tmps != null) {
                for (int i = 0; i < tmps.length; i++) {
                    if (tmps[i].equalsIgnoreCase(extFromFilename)) {
                        return (FileTypeAll) entry.getKey();
                    }
                }
            }
        }
        return FileTypeAll.FILE_TYPE_OTHER;
    }

    /* 特定文件名后面的背景色 */
    private static HashMap<String, String> fileExtToBg = new HashMap<String, String>();

    static {
        addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_AUDIO), "#653ebf");
        addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_VIDEO), "#002d8a");
        addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_IMAGE), "#ffffff");
        addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_TEXT), "#2f93e8");
        addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_WORD), "#2f93e8");
        addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_EXCEL), "#00a51b");
        addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_PPT), "#e87731");
        addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_PDF), "#be1d49");
        addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_APK), "#019eaf");
        addItemBg(fileExtTypes.get(FileTypeAll.FILE_TYPE_WEB), "#e39f18");
    }


    /**
     * 文件中间的大图标
     */
    private static HashMap<String, Integer> fileExtToWhiteIconsBig = new HashMap<String, Integer>();

    static {
        addItemBig(fileExtTypes.get(FileTypeAll.FILE_TYPE_AUDIO), R.drawable.type_audio);
        addItemBig(fileExtTypes.get(FileTypeAll.FILE_TYPE_VIDEO), R.drawable.type_video);
        addItemBig(fileExtTypes.get(FileTypeAll.FILE_TYPE_IMAGE), R.drawable.type_image);
        addItemBig(fileExtTypes.get(FileTypeAll.FILE_TYPE_TEXT), R.drawable.type_word);
        addItemBig(fileExtTypes.get(FileTypeAll.FILE_TYPE_WORD), R.drawable.type_word);
        addItemBig(fileExtTypes.get(FileTypeAll.FILE_TYPE_EXCEL), R.drawable.type_excel);
        addItemBig(fileExtTypes.get(FileTypeAll.FILE_TYPE_PPT), R.drawable.type_ppt);
        addItemBig(fileExtTypes.get(FileTypeAll.FILE_TYPE_PDF), R.drawable.type_pdf);
        addItemBig(fileExtTypes.get(FileTypeAll.FILE_TYPE_APK), R.drawable.type_apk);
        addItemBig(fileExtTypes.get(FileTypeAll.FILE_TYPE_WEB), R.drawable.type_www);
    }

    /**
     * 不同文件类型有不同的背景
     */
    private static void addItemBg(String[] exts, String color) {
        if (exts != null) {
            for (String ext : exts) {
                fileExtToBg.put(ext.toLowerCase(), color);
            }
        }
    }


    private static void addItemBig(String[] exts, int resId) {
        if (exts != null) {
            for (String ext : exts) {
                fileExtToWhiteIconsBig.put(ext.toLowerCase(), resId);
            }
        }
    }

    /**
     * 获取各类型文件的背景图
     *
     * @param theme
     * @param ext
     * @return
     */
    public static int getFileIconBg(int theme, String ext) {
        Integer i = null;
        String color = fileExtToBg.get(ext.toLowerCase());
        if (color == null || color.equals("")) {
            color = "#999999";// 文件夹和未知文件
        }
        i = Color.parseColor(color);
        return i;

    }

    /**
     * 获取各类型文件的大图标
     *
     * @param theme
     * @param ext
     * @return
     */
    public static int getFileIconBig(int theme, String ext) {
        Integer i = null;
        i = fileExtToWhiteIconsBig.get(ext.toLowerCase());
        if (i != null) {
            return i.intValue();
        } else {
            return R.drawable.type_none;
        }

    }

    public void setIcon(EbtFile fileInfo, int flagViewType, TextView textView, ImageView thumbnailInner, ImageView thumbnail) {
        String filePath = fileInfo.path;
        long fileId = fileInfo.dbId;
        String extFromFilename = Util.getExtFromFilename(filePath);
        int idBig = 0;
        if (fileInfo.isFolder) {
            idBig = R.drawable.folder_close;
            thumbnailInner.setImageResource(idBig);
        } else if (fileInfo.type == FileTypeAll.FILE_TYPE_SHORTCUT) {// 快捷方式图标和背景个性设置
            byte[] bb = fileInfo.shortcutIcon;
            if (bb != null && bb.length > 0) {
                thumbnailInner.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeByteArray(bb, 0, bb.length)));
            }
        } else {
            idBig = getFileIconBig(flagViewType, extFromFilename);// 背景
            thumbnailInner.setImageResource(idBig);
        }
        thumbnail.setBackgroundColor(getFileIconBg(flagViewType, extFromFilename));
        mIconLoader.cancelRequest(thumbnail);
        FileTypeAll fc = FileIconHelper.getFileTypeBySuffix(filePath);
        switch (fc) {
            case FILE_TYPE_SHORTCUT:
                thumbnail.setBackgroundColor(Color.parseColor("#f2f1f9"));
                break;
            case FILE_TYPE_APK:
                mIconLoader.loadIcon(thumbnailInner, filePath, fileId, fc);
                break;
            case FILE_TYPE_IMAGE:
                thumbnailInner.setVisibility(View.GONE);
                ImageLoaderUtil.getInstance().loadImage(AppContext.getInstance(), new ImageLoader.Builder().url(filePath).imgView(thumbnail).build());
                break;
            case FILE_TYPE_VIDEO:
                thumbnailInner.setVisibility(View.GONE);
                mIconLoader.loadIcon(thumbnail, filePath, fileId, fc);
                break;
            default:
                break;
        }
    }

    @Override
    public void onIconLoadFinished(ImageView view) {
    }
}
